$(document).ready(function() {
	function getURLVar(key) {
		var value = [];
		
		var query = String(document.location).split('?');
		
		if (query[1]) {
			var part = query[1].split('&');

			for (i = 0; i < part.length; i++) {
				var data = part[i].split('=');
				
				if (data[0] && data[1]) {
					value[data[0]] = data[1];
				}
			}
			
			if (value[key]) {
				return value[key];
			} else {
				return '';
			}
		}
	} 

	// load only if have id menus
	if ($('#menu').length > 0) {
		$('#menu > ul').superfish({
			pathClass	 : 'overideThisToUse',
			delay		 : 0,
			animation	 : {height: 'show'},
			speed		 : 'normal',
			autoArrows   : false,
			dropShadows  : false, 
			disableHI	 : false, /* set to true to disable hoverIntent detection */
			onInit		 : function(){},
			onBeforeShow : function(){},
			onShow		 : function(){},
			onHide		 : function(){}
		});
	}
	$('#menu > ul').css('display', 'block');

	route = getURLVar('route');
	
	if (!route) {
		$('#dashboard').addClass('selected');
	} else {
		part = route.split('/');
		
		url = part[0];
		
		if (part[1]) {
			url += '/' + part[1];
		}
		
		$('a[href*=\'' + url + '\']').parents('li[id]').addClass('selected');
	}
	
	$('#menu ul li').on('click', function() {
		$(this).addClass('hover');
	});

	$('#menu ul li').on('mouseout', function() {
		$(this).removeClass('hover');
	});

	// show and hide password
	$('#hidenPassword').hide();
	$('#hidenPassword').val($('#password').val());
	$('#password').blur(function() {
		$('#hidenPassword').val($(this).val());
	});
	$('#hidenPassword').blur(function() {
		$('#password').val($(this).val());
	});
	showPassword();
	function showPassword () {
		var isShowPassword = $('#showpassword').is(':checked');
		if (isShowPassword) {
			$('#hidenPassword').show();
			$('#password').hide();
		} else {
			$('#hidenPassword').hide();
			$('#password').show();
		}
	}
	$('#showpassword').click (function () {
		showPassword();
	});
	//login validation
	$('#frmLogin .button').click(function() {
		checkvalidation();
	});
	function checkvalidation(){
		var name = $('#txtusername').val();
		var pass = $('#txtpassword').val();
		if(name ==null || name==""){
			alert("Please input username!");
			$('#txtusername').focus();
			return false;
		}else if(pass==null || pass==""){
			alert("Please input password!");
			$('txtpassword').focus();
			return false;
		}
	}
	$('txtusername').select();
	$('#txtusername').focus();

	// hide message
	$("#success").delay(3000);
	$("#success").fadeOut(1000);
	$(".warning").delay(5000);
	$(".warning").fadeOut(2000);

	// date picker
	$( "#datepicker" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd'
	});

	$( ".datepicker" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd'
	});

	$('.openbox').click(function(){
		$(this).next().removeAttr('disabled').focus();
	});

	// disable checkbox in account type
	$('.closebox').click(function(){
		$('.txtpre').attr('disabled','disabled');
		$('.txtpre').val('');	
	});
	
	$('.openbox1').click(function(){
		$(this).next().removeAttr('disabled').focus();
	});
	
	$('.closebox1').click(function(){
		$('.txtexpired').attr('disabled','disabled');
		$('.txtexpired').val('');	
	});	

	$('.openbox2').click(function(){
		$(this).next().removeAttr('disabled').focus();
		$('.home').removeAttr('disabled');
	});
	
	$('.closebox2').click(function(){
		$('.txtbanner').attr('disabled','disabled');
		$('.txtbanner').val('');	
		$('.home').attr('disabled','disabled');
		$('.home').removeAttr('checked');
	});

	// delete product
	$('.delcount').click(function(){
		var id = $(this).attr('id');
		
		if($(".selectCon1").length>1){
			$.ajax({
					type: "GET",
					url: "process.php",
					data: "action=deletet_count&pid=" + id,
					success: function(message){
						$('.'+id).remove();
					}
			});
		}else{
			alert('You cannot delete the last one!');
			$('.delcount').addClass('displayNo');
		}
		
	});
});
function deleteSelected(frm,qstr,itemName,actionFile) {
	qstr = (qstr!="")?"&"+qstr:qstr;
	if(confirmdelcheckeditems(itemName,frm)){
		frm.action=actionFile+".php?action=muliDelete" + qstr;
		frm.submit();
	}
}
function EditSelected(frm,qstr,text,itemName) {
	qstr = (qstr!="")?"&"+qstr:qstr;
	if(confirmEditcheckeditems(itemName,text,frm)){
		frm.action = "index.php?view=add" + qstr;
		frm.submit();
	}
}
function deleteRecord(id){
	if(confirm("Are you sure want to delete this one!")){
		window.location.href="index.php?view=delete&id="+id;
	}
	return false;
}
function validateBanner(){
	var banner=document.forms["frmlist"]["txttitle"].value;
	var addlink=document.forms["frmlist"]["txtaddlink"].value;
	var description=document.forms["frmlist"]["txtdescription"].value;
	if (banner==null || banner=="") {
		alert(" banner title must be filled out");
		return false;
	}
	if (addlink==null || addlink=="") {
		alert(" Addlink must be filled out");
		return false;
	}
}
